package com.situ.staff.employee.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.staff.employee.model.EmployeeModel;
import com.situ.staff.employee.service.Impl.EmployeeServiceImpl;
import com.situ.staff.util.FmtRequest;
import com.situ.staff.util.Upload;

//接受请求，获取参数，封装对象，调用方法，返回结果
@WebServlet("/employee")
public class EmployeeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public EmployeeServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String action = request.getParameter("action");
		Object res = "";
		switch (action) {
		case "reg":
			try {
				res = reg(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "login":
			try {
				res = login(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "list":
			try {
				res = list(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "page":
			try {
				res = page(request);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			break;
		case "add":
			try {
				res = add(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "del":
			try {
				res = del(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "get":
			try {
				res = get(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "upd":
//		case "setDept":
			try {
				res = upd(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "get2":
			try {
				get2(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			request.getRequestDispatcher("/web/page/emp/upd2.jsp").forward(request,response);
			return;
		case "reset":
			try {
				res = reset(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "setDept":
			try {
				res = setDept(request);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			break;
		case "delPic":
			try {
				res = delPic(request);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			break;
		default:
			break;
		}
		FmtRequest.write(response.getWriter(), res);
	}
	private Object delPic(HttpServletRequest request) throws SQLException {
		EmployeeModel model = new EmployeeModel(request.getParameter("code"));
		EmployeeModel mdb = service.selectModel(model);
		String image = mdb.getImage();
		Upload.delFile(image);
		mdb.setImage("");
		return service.update(mdb);
	}

	private String setDept(HttpServletRequest request) throws SQLException {
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
//		System.out.println(model.getCodeDept());
		return service.updateDept(model);
	}

	private Object reset(HttpServletRequest request) throws SQLException {
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
		return service.update(model);
	}

	//分页的操作
	private Object page(HttpServletRequest request) throws SQLException {
		
		Map<String,String> fields = new HashMap<>();
		fields.put("code","code");
		fields.put("name","name");
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class,fields);
		//上面这行代码映射完后，model中code,name属性即为搜索输入框中的输入
		String pageIndex = request.getParameter("pageIndex");
		String pageLimit = request.getParameter("pageLimit");
//		System.out.println(pageIndex);
//		System.out.println(pageLimit);
//		System.out.println(model.toString());
//		System.out.println(model.getCode());
//		System.out.println(model.getName());
		//设置分页相关属性
		model.setPageIndex(Integer.parseInt(pageIndex));
		model.setPageLimit(Integer.parseInt(pageLimit));
		model.setPageOn(true);
		//查询出符合检索条件和分页条件的记录，分页条件即满足limit子句的记录
		List<EmployeeModel> list = service.selectList(model);
//		for(EmployeeModel e : list) {
//			System.out.println(e.toString());
//		}
		//查询出符合检索条件的记录总的条数，用于分页时的count属性赋值
		Integer count = service.selectCount(model);
		//将上述两个属性封装到map,返回给网页
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list",list);
		map.put("count",count);
		return map;//
	}

	//采用servlet,将数据先保存到域中，后面再从域中取出，回显到弹出层表单中
	private void get2(HttpServletRequest request) throws SQLException {
		EmployeeModel model = new EmployeeModel(request.getParameter("code"));
		EmployeeModel m1 =  service.selectModel(model);
		request.getSession().setAttribute("m1", m1);
	}

	private Object upd(HttpServletRequest request) throws SQLException {
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
		return service.update(model);
	}
	//采用ajax
	private EmployeeModel get(HttpServletRequest request) throws SQLException {
		EmployeeModel model = new EmployeeModel(request.getParameter("code"));
		return service.selectModel(model);
	}

	private Object del(HttpServletRequest request) throws SQLException {
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
		return service.delete(model);
	}

	private Object add(HttpServletRequest request) throws SQLException {
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);

		return service.insert(model);
	}

	EmployeeServiceImpl service = new EmployeeServiceImpl();

	private List<EmployeeModel> list(HttpServletRequest request) throws SQLException {
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
		return service.selectList(model);
	}

	private String login(HttpServletRequest request) throws SQLException {
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
		String res  = service.login(model);
//		System.out.println(res);
//		System.out.println(service.selectModel(model).toString());
		if("success".equals(res)) {
			request.getSession().setAttribute("user", service.selectModel(model));
		}
		return res;
	}

	public String reg(HttpServletRequest request) throws SQLException {

//		String code = request.getParameter("code");
//		String pass = request.getParameter("pass");
//		String name = request.getParameter("name");
//		EmployeeModel model = new EmployeeModel(code, name, pass);
		EmployeeModel model = FmtRequest.parseModel(request, EmployeeModel.class);
//		request.setAttribute("user", model);
		return service.insert(model);

	}

}
